package cn.iocoder.yudao.module.system.api.cp;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.cp.dto.RadarSaveReqVO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptSaveReqVO;
import cn.iocoder.yudao.module.system.enums.cp.CpEnum;
import cn.iocoder.yudao.module.system.framework.cp.config.WxCpConfiguration;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMessageService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static me.chanjar.weixin.common.api.WxConsts.KefuMsgType.*;
import static me.chanjar.weixin.common.api.WxConsts.MaterialType.NEWS;

/**
 * @Author LiZhiMin
 * @Date 2024/8/15 10:15
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class RadarApiImpl implements RadarApi{

    @Override
    public CommonResult<Boolean> sendRadarMessage(RadarSaveReqVO vo) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日 HH:mm");
        String formattedDate = now.format(formatter);
        String title = vo.getTitle();
        String name = vo.getName();

        WxCpService wxCpService = WxCpConfiguration.getCpService(CpEnum.valueOfTenantId(vo.getTenantId()).getCorpId(), CpEnum.valueOfTenantId(vo.getTenantId()).getAgentId());
        WxCpMessageService wxCpMessageService = new WxCpMessageServiceImpl(wxCpService);
        WxCpMessage wxCpMessage = new WxCpMessage();
        wxCpMessage.setToUser(vo.getToUser());

       /* Long tenantId = TenantContextHolder.getTenantId();
        wxCpService = WxCpConfiguration.getCpService(CpEnum.valueOfTenantId(Math.toIntExact(tenantId)).getCorpId(),
                CpEnum.valueOfTenantId(Math.toIntExact(tenantId)).getAgentId());*/

        //wxCpMessage.setAgentId(1000007);
        wxCpMessage.setMsgType(TEXT);
        try{
            String bean="访问雷达 \n"+formattedDate;
            String content;
            switch (vo.getRadarType()) {
                case "0":
                    content = String.format(
                            "%s \n %s正在浏览 \"文章 | %s\"",
                            bean, name, title
                    );
                    break;
                case "1":
                    content = String.format(
                            "%s \n %s  转发了您的 \"文章 | %s\"",
                            bean, name, title
                    );
                    break;
                default:
                    content = String.format(
                            "%s \n %s 完成浏览 \"文章 | %s\"" +
                                    "浏览时长 %d 秒; 浏览至 %d.0%%",
                            bean, name, title, vo.getLookTime(), vo.getPercentage()
                    );
                    break;
            }
            wxCpMessage.setContent(content);
            WxCpMessageSendResult sendResult = wxCpMessageService.send(wxCpMessage);
            return CommonResult.success(sendResult.getErrCode() == 0);
        }catch (WxErrorException e){
            throw new RuntimeException("Failed to send message666", e);
        }
    }
}
